﻿package com.guavestudios.fcon.plugins {
	import com.guavestudios.fcon.api.IControlableConsole;
	import com.guavestudios.fcon.commands.ScriptCommand;
	import com.guavestudios.fcon.plugins.script.Script;
	import flash.events.TimerEvent;
	import flash.net.SharedObject;
	import flash.utils.Timer;
	
	/**
	* ...
	* @author Default
	*/
	public class SimpleScripter implements IConsolePlugin{
		
		private static const SHARED_NAME:String = "__scripts";
		private var console:IControlableConsole;
		private var scripts:Object;
		
		private var currentScript:Script;
		private var currentLine:int = 0;
		private var sleepTimer:Timer;
		private var _saveScript:Boolean = false;
		
		private var shared:SharedObject;
		
		public function SimpleScripter() {
			scripts = new Object();
			sleepTimer = new Timer(1000, 1);
			sleepTimer.addEventListener(TimerEvent.TIMER, sleepTimer_complete);
		}
		
		private function sleepTimer_complete(e:TimerEvent):void {
			currentLine++;
			executeScript(currentScript.name, currentLine);
		}
		public function getList():Array
		{
			var a:Array = [];
			for each (var sc:Script in scripts)
			{
				a.push(sc);
			}
			return a;
		}
		public function removeScript(id:String):void
		{
			delete scripts[id];
			saveObject();
		}
		public function getScript(id:String):Script
		{
			return scripts[id];
		}
		public function addScript(sc:Script):void
		{
			scripts[sc.name] = sc;
			saveObject();
		}
		public function hasScript(id:String):Boolean
		{
			return (scripts[id] != undefined);
		}
		public function executeScript(id:String,line:int=0):void
		{
			var sc:Script = getScript(id);
			if (sc == null)
				return;
				
			var lns:Array = sc.getLines();
			if (lns.length > 0)
			{
				var l:String;
				for (var i:int = line, len:int = lns.length; i<len; i++)
				{
					l = lns[i];
					if (l.substring(0, 5) == "sleep")
					{
						var time:int = parseInt(l.substr(6));
						currentLine = i;
						currentScript = sc;
						sleepTimer.delay = time;
						sleepTimer.reset();
						sleepTimer.start();
						break;
					}
					else
					{
						console.execute(l);
					}
					
				}
			}
			
		}
		
		/* INTERFACE com.guavestudios.fcon.plugins.IConsolePlugin */
		
		public function get State():int{
			return 0;
		}
		
		public function get Name():String{
			return "SimpleScripter";
		}
		
		public function get Version():String{
			return "0.0";
		}
		
		public function initPlugin(c:IControlableConsole):void{
			console = c;
			
			c.addCommand(new ScriptCommand());
			
			_saveScript=String(c.getConfig("Script.SaveScript")) == "true";
			shared = SharedObject.getLocal("__fcon");
			var data:Object = shared.data[SHARED_NAME];
			if (data != null)
			{
				var a:Array;
				var sc:Script;
				for (var i:String in data)
				{
					a = data[i];
					sc = new Script();
					sc.name = i;
					sc.setScript(a);
					addScript(sc);
				}
			}
		}
		public function saveObject():void
		{
			if (_saveScript)
			{
				var o:Object = new Object();
				var filled:Boolean = false;
				for each (var s:Script in scripts)
				{
					o[s.name] = s.getLines();
					filled = true;
				}
				shared.data[SHARED_NAME]=o;
			}
			
			
		}
		
		public function unloadPlugin():void {
				
			console = null;
		}
		
	}
	
}